package com.eos.common.tool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eos.business.service.SysCustomerBusinessService;
import com.eos.business.service.SysUserBusinessService;
import com.eos.orm.mybatis.model.SysCustomer;
import com.eos.orm.mybatis.model.SysMenu;
import com.eos.orm.mybatis.model.SysRole;
import com.eos.orm.mybatis.model.SysUser;
import com.eos.orm.mybatis.model.ViewRoleMenu;

public class DataCacheUtil {
	private static final Map<String, List<SysUser>> SYSUSER_CACHE_MAP = new HashMap<String, List<SysUser>>();
	public static final Map<String, List<SysMenu>> SYSMENU_CACHE_MAP = new HashMap<String, List<SysMenu>>();
	private static final Map<String, List<SysRole>> SYSROLE_CACHE_MAP = new HashMap<String, List<SysRole>>();
	private static final Map<String, List<ViewRoleMenu>> VIEWROLEMENU_CACHE_MAP = new HashMap<String, List<ViewRoleMenu>>();
	private static final Map<String, List<SysCustomer>>	SYSCUSTOMER_CACHE_MAP = new HashMap<String, List<SysCustomer>>();
	
	/**
	 * 系统启动时调用, 对map进行初始化
	 */
	public static void initCacheMap(){
		sysUserCache();
		sysMenuCache();
		sysRoleCache();
		viewRoleMenuCache();
		sysCustomerCache();
	}
	
	private static void sysUserCache() {
		if (SYSUSER_CACHE_MAP.isEmpty()) {
			synchronized (SYSUSER_CACHE_MAP) {
				if (SYSUSER_CACHE_MAP.isEmpty()) {
					try{
						SysUserBusinessService sbs = new SysUserBusinessService();
						SysUser user = new SysUser();
						user.setDealFlag(ConstantTool.DEAL_FALG_OK);
						List<SysUser> userList = sbs.findSysUserByCondition(user);
						SYSUSER_CACHE_MAP.put("SysUser", userList);
					}catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	/**
	 * 获取系统中所有的用户
	 * @return
	 */
	public static List<SysUser> getAllSysUser(){
		sysUserCache();
		if(SYSUSER_CACHE_MAP.isEmpty())
			sysUserCache();
		return SYSUSER_CACHE_MAP.get("SysUser");
	}
	
	/**
	 * 通过ID获取用户信息
	 * @param userId
	 * @return
	 */
	public static SysUser getSysUser(int userId){
		List<SysUser> uList = getAllSysUser();
		for(SysUser u : uList) {
			if(u.getId() == userId){
				return u;
			}
		}
		return null;
	}
	
	/**
	 * 通过邮箱, 获取用户信息
	 * @param email
	 * @return
	 */
	public static SysUser getSysUser(String email){
		List<SysUser> uList = getAllSysUser();
		for(SysUser u : uList) {
			if(u.getUserEmail().equals(email)){
				return u;
			}
		}
		return null;
	}
	
	public static void clearSysUserMap(){
		SYSUSER_CACHE_MAP.clear();
	}
	
	private static void sysMenuCache() {
		if (SYSMENU_CACHE_MAP.isEmpty()) {
			synchronized (SYSMENU_CACHE_MAP) {
				if (SYSMENU_CACHE_MAP.isEmpty()) {
					try{
						SysUserBusinessService sbs = new SysUserBusinessService();
						List<SysMenu> menuList = sbs.findAllSysMenu();
						SYSMENU_CACHE_MAP.put("SysMenu", menuList);
					}catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public static void clearSysMenuMap(){
		SYSMENU_CACHE_MAP.clear();
	}
	
	/**
	 * 从缓存中获取系统所有的菜单
	 * @return
	 */
	public static List<SysMenu> getAllSysMenu(){
		sysMenuCache();
		if(SYSMENU_CACHE_MAP.isEmpty())
			sysMenuCache();
		return SYSMENU_CACHE_MAP.get("SysMenu");
	}
	/**
	 * 缓存系统所有角色
	 */
	private static void sysRoleCache() {
		if (SYSROLE_CACHE_MAP.isEmpty()) {
			synchronized (SYSROLE_CACHE_MAP) {
				if (SYSROLE_CACHE_MAP.isEmpty()) {
					try{
						SysUserBusinessService sbs = new SysUserBusinessService();
						List<SysRole> menuList = sbs.findAllSysRole();
						SYSROLE_CACHE_MAP.put("SysRole", menuList);
					}catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public static void clearSysRoleMap(){
		SYSROLE_CACHE_MAP.clear();
	}
	/**
	 * 获取系统所有角色信息
	 * @return
	 */
	public static List<SysRole> getAllSysRole(){
		sysRoleCache();
		if(SYSROLE_CACHE_MAP.isEmpty())
			sysRoleCache();
		return SYSROLE_CACHE_MAP.get("SysRole");
	}

	public static SysRole getSysRole(int roleId){
		 List<SysRole> roleList = getAllSysRole();
		 for(SysRole role : roleList){
			 if(roleId == role.getId()){
				 return role;
			 }
		 }
		 return null;
	}
	
	private static void viewRoleMenuCache() {
		if (VIEWROLEMENU_CACHE_MAP.isEmpty()) {
			synchronized (VIEWROLEMENU_CACHE_MAP) {
				if (VIEWROLEMENU_CACHE_MAP.isEmpty()) {
					try{
						SysUserBusinessService sbs = new SysUserBusinessService();
						List<ViewRoleMenu> menuList = sbs.findAllRoleMenu();
						VIEWROLEMENU_CACHE_MAP.put("ViewRoleMenu", menuList);
					}catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public static void clearViewRoleMenuMap(){
		VIEWROLEMENU_CACHE_MAP.clear();
	}
	
	/**
	 * 系统角色菜单视图
	 * @return
	 */
	public static List<ViewRoleMenu> getAllViewRoleMenu(){
		viewRoleMenuCache();
		if(VIEWROLEMENU_CACHE_MAP.isEmpty())
			viewRoleMenuCache();
		return VIEWROLEMENU_CACHE_MAP.get("ViewRoleMenu");
	}
	
	/**
	 * 获取角色对应的菜单
	 * @param roleId  角色ID
	 */
	public static List<ViewRoleMenu> getSysMenuOfRole(int roleId){
		List<ViewRoleMenu> list = getAllViewRoleMenu();
		List<ViewRoleMenu> rtnList = null;
		if(null != list && list.size() >0){
			rtnList = new ArrayList<ViewRoleMenu>();
			for(ViewRoleMenu vrm : list){
				if(roleId == vrm.getRoleId()){
					rtnList.add(vrm);
				}
			}
		}
		return rtnList;
	}
	
	private static void sysCustomerCache(){
		if (SYSCUSTOMER_CACHE_MAP.isEmpty()) {
			synchronized (SYSCUSTOMER_CACHE_MAP) {
				if (SYSCUSTOMER_CACHE_MAP.isEmpty()) {
					try{
						SysCustomerBusinessService scbs = new SysCustomerBusinessService();
						List<SysCustomer> customerList = scbs.getAllSyscutomer();
						SYSCUSTOMER_CACHE_MAP.put("SysCustomer", customerList);
					}catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public static void clearSysCustomerMap(){
		SYSCUSTOMER_CACHE_MAP.clear();
	}
	
	public static List<SysCustomer> getAllSysCustomer(){
		sysCustomerCache();
		if(SYSCUSTOMER_CACHE_MAP.isEmpty()) 
			sysCustomerCache();
		return SYSCUSTOMER_CACHE_MAP.get("SysCustomer");
	}
	
	public static SysCustomer getSysCustomer(int customerId){
		List<SysCustomer> cList = getAllSysCustomer();
		for(SysCustomer c : cList){
			if(c.getId().intValue() == customerId){
				return c;
			}
		}
		return null;
	}
}
